$(function () {
  xiangmuyusuan = 1;
  initxiangmuyusuanData();
})

function initxiangmuyusuanData() {
  const username = localStorage.getItem("user");
  const currYear = localStorage.getItem("year");
  const currMon = localStorage.getItem("month");
  const currDept = localStorage.getItem("dept");
  //数字
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 17,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    $(".shoukuan").text(pricefmt((chartData[0].key01 / 10000).toFixed(2)));
    $(".yusuan").text(pricefmt((chartData[0].key02 / 10000).toFixed(2)));
    $(".zhichu").text(pricefmt((chartData[0].key03 / 10000).toFixed(2)));
  });
  // 项目成本预算执行情况｜类别对比
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 18,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, "项目成本预算执行情况｜类别对比");
    getleibieChart(chartData) //类别对比
  });

  // 项目成本预算执行情况
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 16,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, "项目成本预算执行情况");
    getleijiChart(chartData);
  });
  // 各事业部项目成本预算执行情况
  var obj = {
    username: username,
    budgetYear: currYear,
    budgetMonth: currMon,
    dept: currDept,
    status: 2,
    chartPage: 60101,
    chartNo: 19,
  };
  ApiGet("getdata", obj, function (e) {
    var json = JSON.parse(e);
    var chartData = json.items[0].charts[0].data;
    console.log(chartData, "各事业部项目成本预算执行情况");
    getallChart(chartData);
  });
}

function getleibieChart(data) {
  let xData = []
  let xiangmushoukuan = [];
  let chengbenyusuan = [];
  let chengbenzhichu = [];

  data.forEach((item) => {
    xData.push(item.key01);
    xiangmushoukuan.push((item.key02 / 10000).toFixed(2));
    chengbenyusuan.push((item.key03 / 10000).toFixed(2));
    chengbenzhichu.push((item.key04 / 10000).toFixed(2));
  });

  let legendData = ["项目收款", "成本预算", "成本支出"];
  let Data = [{
      name: "项目收款",
      type: "bar",
      barWidth: "25",
      barGap: 0,
      itemStyle: {
        color: "#165DFF",
      },
      data: xiangmushoukuan,
    },
    {
      name: "成本预算",
      type: "bar",
      barWidth: "25",
      barGap: 0,
      itemStyle: {
        color: "#F7BA1E",
      },
      data: chengbenyusuan,
    },
    {
      name: "成本支出",
      type: "bar",
      barWidth: "25",
      barGap: 0,
      itemStyle: {
        color: "#20C997",
      },
      data: chengbenzhichu,
    },
  ]
  have('leibie') ? yueduqushi('leibie', xData, legendData, Data) : ''
}

function getleijiChart(data) {
  let legendData = ['项目收款', '成本预算', '成本支出']
  let xiangmushoukuan = [];
  let chengbenyusuan = [];
  let chengbenzhichu = [];
  for (let i = 0; i < data.length; i++) {
    xiangmushoukuan.push((data[i].key02 / 10000).toFixed(1))
    chengbenyusuan.push((data[i].key03 / 10000).toFixed(1))
    chengbenzhichu.push((data[i].key04 / 10000).toFixed(1))
  }
  let leijirate = [{
      name: '项目收款',
      smooth: true, //平滑曲线显示
      symbol: 'circle', //标记的图形为实心圆
      symbolSize: 8, //标记的大小
      itemStyle: {
        normal: {
          color: '#165DFF',
        },
      },
      showSymbol: false,
      type: 'line',
      lineStyle: {
        width: 2
      },
      data: xiangmushoukuan
    },
    {
      name: '成本预算',
      smooth: true, //平滑曲线显示
      symbol: 'circle', //标记的图形为实心圆
      symbolSize: 8, //标记的大小
      itemStyle: {
        normal: {
          color: '#F7BA1E',
        },
      },
      showSymbol: false,
      lineStyle: {
        width: 2
      },
      type: 'line',
      data: chengbenyusuan
    },
    {
      name: '成本支出',
      smooth: true, //平滑曲线显示
      symbol: 'circle', //标记的图形为实心圆
      symbolSize: 8, //标记的大小
      itemStyle: {
        normal: {
          color: '#20C997',
        },
      },
      showSymbol: false,
      lineStyle: {
        width: 2
      },
      type: 'line',
      data: chengbenzhichu
    }
  ]
  have('leiji') ? lineChartMD('leiji', leijirate, legendData) : ''
}

function getallChart(data) {
  let xData = [];
  let chengbenyusuan = []; //成本预算
  let shiji = []; //实际
  for (let i = 0; i < data.length; i++) {
    xData.push(data[i].key01);
    chengbenyusuan.push((data[i].key02 / 10000).toFixed(2));
    shiji.push((data[i].key03 / 10000).toFixed(2));
  }

  let legendData = ["成本预算", "实际"];
  let Data = [{
      name: "成本预算",
      type: "bar",
      barWidth: "45",
      barGap: 0,
      itemStyle: {
        color: "#F7BA1E",
      },
      label: {
        show: true,
        position: 'top',
        distance: 10,
        color: '#1D2129',
        fontWeight: 'bold'
      },
      data: chengbenyusuan,
    },
    {
      name: "实际",
      type: "bar",
      barWidth: "45",
      barGap: 0.25,
      itemStyle: {
        color: "#20C997",
      },
      label: {
        show: true,
        position: 'top',
        distance: 10,
        color: '#1D2129',
        fontWeight: 'bold'
      },
      data: shiji,
    },
  ]
  have('all') ? yueduqushi('all', xData, legendData, Data) : ''
}